package com.lhkj.iams.wechat.controller;

import com.alibaba.fastjson2.JSON;
import com.lhkj.iams.base.ro.ActionResult;
import com.lhkj.iams.wechat.config.mp.bo.WxMpMessageBo;
import com.lhkj.iams.wechat.service.WxMpAuthService;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("api/iams/mp")
public class WxMpController {

    protected Logger log = LoggerFactory.getLogger(getClass());

    @Resource
    private WxMpAuthService wxMpAuthService;

    /**
     * 授权获取code
     *
     * @param redirectUrl
     * @return
     */
    @GetMapping("/authorize")
    public String authorize(@RequestParam(value = "redirect_url", defaultValue = "/ct/api/wechat/auth/wechatLogin") String redirectUrl,
                          HttpServletResponse response) throws IOException {
        log.debug("前置机收到重定向地址：{}", redirectUrl);
        return wxMpAuthService.authorize(redirectUrl, response);
    }

    /**
     * 根据code获取微信用户信息
     *
     * @param code
     * @return
     * @throws WxErrorException
     */
    @ResponseBody
    @GetMapping("userInfo")
    public ActionResult<WxOAuth2UserInfo> wxOpenIdByCode(@RequestParam("code")String code,@RequestParam(name = "mpId", required = false) String mpId) throws WxErrorException {
        return ActionResult.success(wxMpAuthService.wxOpenIdByCode(code, mpId));
    }

    /**
     * 调起微信扫码
     * @param mpId
     * @return
     * @throws WxErrorException
     */
    @ResponseBody
    @GetMapping("JsapiSignature")
    public ActionResult<WxJsapiSignature> createJsapiSignature(@RequestParam(name = "mpId", required = false) String mpId, @RequestParam(name = "url") String url) throws WxErrorException {
        return ActionResult.success(wxMpAuthService.createJsapiSignature(mpId, url));
    }

    /**
     * 微信小程序事件推送
     *
     * @return
     */
    @ResponseBody
    @PostMapping("receiveMessage")
    public String doPost(HttpServletResponse response,
                         HttpServletRequest request,
                         @RequestBody String requestBody,
                         @RequestParam("mp_id") String mpId,
                         @RequestParam("signature") String signature,
                         @RequestParam("timestamp") String timestamp,
                         @RequestParam("nonce") String nonce,
                         @RequestParam(name = "encrypt_type", required = false) String encType,
                         @RequestParam(name = "msg_signature", required = false) String msgSignature) {
        WxMpMessageBo wxMpMessageBo = new WxMpMessageBo();
        wxMpMessageBo.setMpId(mpId);
        wxMpMessageBo.setMessage(requestBody);
        wxMpMessageBo.setMsgSignature(msgSignature);
        wxMpMessageBo.setEncType(encType);
        wxMpMessageBo.setTimestamp(timestamp);
        wxMpMessageBo.setNonce(nonce);
        log.info("前置机收到微信客服消息：{}", JSON.toJSONString(wxMpMessageBo));
        return wxMpAuthService.receiveMessage(wxMpMessageBo);
    }



}
